﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace LAN_Party_Manager
{

    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmAll manager = new frmAll();
            manager.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmActive actmanager = new frmActive();
            actmanager.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Player.activePlayers == 0)
            {
                MessageBox.Show("No active players.", "Error");
                return;
            }
            frmGame newgame = new frmGame();
            if (newgame.ShowDialog(this) == DialogResult.OK)
            {
                lstTeam1.Items.Clear();
                lstTeam2.Items.Clear();
                if (Player.players.Count > 0)
                    foreach (Player p in Player.players)
                    {
                        if (p.getTeam() == 1)
                            lstTeam1.Items.Add(p.getName());
                        else if (p.getTeam() == 2)
                            lstTeam2.Items.Add(p.getName());
                    }
                btnFinish.Enabled = true;
                btnReset.Enabled = true;
                Player.gameTime = 0;
                tmrGame.Start();
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            tmrGame.Stop();
            Player.team1elo = 0;
            Player.team2elo = 0;
            int winner = 0;
            double[] chances = new double[2];
            int[] telo = new int[2];
            if (rdoTeam1.Checked)
                winner = 1;
            else if (rdoTeam2.Checked)
                winner = 2;
            else
            {
                MessageBox.Show("Please select a winner.");
                return;
            }
            foreach (Player p in Player.players)
            {
                if (p.getTeam() == 1)
                    Player.team1elo += p.getElo();
                else if (p.getTeam() == 2)
                    Player.team2elo += p.getElo();
            }
            Player.team1elo = Player.team1elo / lstTeam1.Items.Count;
            Player.team2elo = Player.team2elo / lstTeam2.Items.Count;
            chances[0] = 1 / (1 + Math.Pow(10, (Player.team2elo - Player.team1elo) / 400));
            chances[1] = 1 / (1 + Math.Pow(10, (Player.team1elo - Player.team2elo) / 400));

            foreach (Player p in Player.players)
            {
                if(p.isActive())
                {
                    if (p.getTeam() == winner)
                    {
                        p.setElo((int)(p.getElo() + (32 - Math.Max(8, p.getElo() - p.getEnemyTeamElo())) * (1 - chances[p.getTeam() - 1])));
                        if (rdoYes.Checked)
                            p.addTime(Player.gameTime);
                        else if (rdoCustom.Checked)
                            p.addTime(Convert.ToInt32(txtTime.Text));
                    }
                    else
                        p.setElo((int)(p.getElo() + (32 - Math.Max(8, p.getElo() - p.getEnemyTeamElo())) * (0 - chances[p.getTeam() - 1])));
                }
 
            }
            resetAll();
        }

        private void resetAll()
        {
            lstTeam1.Items.Clear();
            lstTeam2.Items.Clear();
            rdoTeam1.Checked = false;
            rdoTeam2.Checked = false;
            foreach (Player p in Player.players)
            {
                p.setTeam(0);
            }
            btnFinish.Enabled = false;
            btnReset.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            resetAll();
            foreach (Player p in Player.players)
            {
                if (p.isActive())
                {
                    lstTeam1.Items.Add(p.getName());
                    lstTeam2.Items.Add(p.getElo());
                }
            }
            btnReset.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            resetAll();
            tmrGame.Stop();
            Player.gameTime = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resetAll();
        }

        private void exportCurrentDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFormatter formatter = new BinaryFormatter();
            if (sfdSave.ShowDialog() == DialogResult.OK)
            {
                Stream stream = new FileStream(sfdSave.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, Player.players);
                stream.Close();
            }
        }

        private void importDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFormatter formatter = new BinaryFormatter();
            if (sfdOpen.ShowDialog() == DialogResult.OK)
            {
                Stream stream = new FileStream(sfdOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                Player.players = (List<Player>)formatter.Deserialize(stream);
                stream.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void tmrGame_Tick(object sender, EventArgs e)
        {
            Player.gameTime++;
            groupBox2.Text = "Record Time? - " + Player.gameTime + " min";
        }

        private void txtTime_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyValue < 48 || e.KeyValue > 57) && e.KeyCode != Keys.Back )
                e.SuppressKeyPress = true;
        }
    }

}
